package newcrunch.gui;

import compiler.ClassFileManager;
import compiler.DynamicJava;
import compiler.patch.CompilerChecker;
import compiler.patch.CustomToolProvider;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.swing.SwingWorker;
import newcrunch.util.Logging;

/**
 *
 * @author ccadm
 */
public class CompilerManager 
{
    private RightTab rightTab;
    private ClassLoader loader;
    
    public void setRightTab(final RightTab tab)
    {
        rightTab = tab;
        tab.getCompileButton().addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent evt)
            {
                String code = tab.getEditor().getText();
                
                //patch for systems that do not have tools.jar in their JRE
                if (DynamicJava.compiler == null)
                {
                    if (CustomToolProvider.getSystemJavaCompiler() != null)
                        DynamicJava.compiler =  CustomToolProvider.getSystemJavaCompiler();
                    
                    else
                    {
                        tab.getCompileButton().setEnabled(false);
                        //start a new thread
                        SwingWorker<Object, Void> worker = new SwingWorker<Object, Void>()
                        {
                            @Override
                            public Object doInBackground()
                            {
                                CompilerChecker checker = CompilerChecker.getInstance();
                                tab.getCompileButton().setEnabled(!checker.check() || checker.hasToolsJarInLib());
                                return null;
                            }
                        };
                        worker.execute();
                    }
                }
                
                if (tab.getCompileButton().isEnabled())
                {
                    String output = compile(code);
                    if ("".equals(output))
                        output = "Compilation Success.";
                    rightTab.getCompilerOutput().setText(output);
                    focusCompileTab();

                    InterpreterManager manager = rightTab.getInterpreterManager();
                    manager.setInterpreterClassLoader(loader);
                    manager.resetOutput();
                }
            }
        });
    }
    
    private void focusCompileTab()
    {
        rightTab.focusCompileTab();
    }
    
    public String compile(String text)
    {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        
        try {
            String filename = rightTab.getFilename();
            ClassFileManager result = DynamicJava.getClassManager(filename, text, bos);
            loader = result.getClassLoader();
            bos.close();
        } catch (IOException e) {
            Logging.log(e);
        }
        
        return bos.toString();
    }
    
    public ClassLoader getClassLoader()
    {
        return loader;
    }
}
